<template>
  <div class="container">
    <div class="app-container">
      <el-tree
        :data="list"
        :props="defaultProps"
        default-expand-all
        :expand-on-click-node="false"
      >
        <template slot-scope="{data}">
          <el-row
            type="flex"
            justify="space-betweer"
            align="middle"
            style="width:100%;height:40px"
          >
            <el-col>{{ data.name }}</el-col>
            <el-col :span="6">
              <span class="tree-manager">{{ data.managerName }}</span>
              <el-dropdown @command="handleCommand($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="editor">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="remove">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 弹出框  用插件做 -->
    <add-dept
      ref="adddept"
      :dialog-visible="dialogVisible"
      :fuid="fuid"
      @dialogVisible="xx"
      @refresh="getDashboard"
    />
  </div>
</template>
<script>
import { getDashboard, deleteDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import addDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: { addDept },
  data() {
    return {
      list: [], // 渲染页面用
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dialogVisible: false, // 控制子组件的开关
      fuid: 0
    }
  },
  created() {
    this.getDashboard()
  },
  methods: {
    async getDashboard() {
      this.list = transListToTreeData(await getDashboard(), 0)
      console.log(this.list)
    },
    handleCommand(command, id) {
      if (command === 'add') {
        this.dialogVisible = true
        this.fuid = id
      } else if (command === 'editor') {
        this.dialogVisible = true
        this.fuid = id
        // 现在需要调个接口把数据传给子组件，但是子组件不好接，所有直接调子组件的方法
        // 等上面数据传完了再调
        this.$nextTick(() => {
          this.$refs.adddept.getDepartmentDetail()
        })
      } else {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        }).then(async() => {
          await deleteDepartment(id)
          this.getDashboard()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }
    },
    xx(i) {
      this.dialogVisible = false
    }
  }
}
</script>
<style scoped>
 .tree-manager{
  margin-left: 50px;
  margin-right: 20px;
   display: inline-block;
    width: 50px;
 }
 .app-container{
  padding: 30px 140px;
  font-size: 14px;
}
</style>
